﻿Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Windows.Forms
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
<Serializable()> Public Class BackImage
    Private m_InsertImageCollection As New List(Of CImPortImage.InsertImage)
    Property InsertImageCollection() As List(Of CImPortImage.InsertImage)
        Get
            Return Me.m_InsertImageCollection
        End Get
        Set(ByVal value As List(Of CImPortImage.InsertImage))
            Me.m_InsertImageCollection = value
        End Set
    End Property
    Public Function Save(ByVal path As String, ByVal Name As String) As Boolean
        Try
            ' Create a filestream object
            Dim fileStr As Stream = File.Open(path & Name, FileMode.Create)
            ' Create a linked list object and populate it with random nodes
            Dim ObjBackImage As New BackImage
            ObjBackImage = Me
            ' Create a formatter object based on command line arguments
            Dim formatter As IFormatter
            formatter = CType(New BinaryFormatter, IFormatter)
            ' Serialize the object graph to stream
            formatter.Serialize(fileStr, ObjBackImage)
            ' All done
            fileStr.Close()
        Catch ex As Exception

            Return Nothing
        End Try

    End Function
    Public Function Open(ByVal path As String, ByVal Name As String) As Boolean
        Try
            Dim fileStr As Stream = File.Open(path & Name, FileMode.Open)

            ' Create a formatter object based on command line arguments
            Dim formatter As IFormatter

            formatter = CType(New BinaryFormatter, IFormatter)
            ' Deserialize the object graph from stream
            Dim ObjBackImage As New BackImage
            ObjBackImage = CType(formatter.Deserialize(fileStr), BackImage)
            fileStr.Close()
            Me.m_InsertImageCollection = ObjBackImage.InsertImageCollection
            Return True
        Catch ex As Exception
            MsgBox("打开背景图出错，请查看对应的文件在不在" & Name & "相应的目录下!")
            Return False
        End Try
    End Function
    <OnDeserialized()> _
    Friend Sub OnDeserializedMethod(ByVal context As StreamingContext)
        If Me.m_InsertImageCollection Is Nothing Then
            m_InsertImageCollection = New List(Of CImPortImage.InsertImage)
        End If
    End Sub

End Class
